{
 "nbformat": 4,
 "nbformat_minor": 2,
 "metadata": {
  "language_info": {
   "name": "python",
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "version": "3.8.1-final"
  },
  "orig_nbformat": 2,
  "file_extension": ".py",
  "mimetype": "text/x-python",
  "name": "python",
  "npconvert_exporter": "python",
  "pygments_lexer": "ipython3",
  "version": 3,
  "kernelspec": {
   "name": "python38164bitec4538a0ed7a4029b9bd19594323cc7e",
   "display_name": "Python 3.8.1 64-bit"
  }
 },
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd \n",
    "import _pickle as cPickle"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>user_id</th>\n      <th>location</th>\n      <th>age</th>\n      <th>country</th>\n      <th>age_group</th>\n      <th>isbn</th>\n      <th>rating</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>0</th>\n      <td>2</td>\n      <td>stockton, california, usa</td>\n      <td>18.0</td>\n      <td>usa</td>\n      <td>3</td>\n      <td>0195153448</td>\n      <td>0</td>\n    </tr>\n    <tr>\n      <th>1</th>\n      <td>7</td>\n      <td>washington, dc, usa</td>\n      <td>NaN</td>\n      <td>usa</td>\n      <td>0</td>\n      <td>034542252</td>\n      <td>0</td>\n    </tr>\n    <tr>\n      <th>2</th>\n      <td>8</td>\n      <td>timmins, ontario, canada</td>\n      <td>NaN</td>\n      <td>canada</td>\n      <td>0</td>\n      <td>0002005018</td>\n      <td>5</td>\n    </tr>\n    <tr>\n      <th>3</th>\n      <td>8</td>\n      <td>timmins, ontario, canada</td>\n      <td>NaN</td>\n      <td>canada</td>\n      <td>0</td>\n      <td>0060973129</td>\n      <td>0</td>\n    </tr>\n    <tr>\n      <th>4</th>\n      <td>8</td>\n      <td>timmins, ontario, canada</td>\n      <td>NaN</td>\n      <td>canada</td>\n      <td>0</td>\n      <td>0374157065</td>\n      <td>0</td>\n    </tr>\n    <tr>\n      <th>...</th>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n    </tr>\n    <tr>\n      <th>1149767</th>\n      <td>278854</td>\n      <td>portland, oregon, usa</td>\n      <td>NaN</td>\n      <td>usa</td>\n      <td>0</td>\n      <td>0425163393</td>\n      <td>7</td>\n    </tr>\n    <tr>\n      <th>1149768</th>\n      <td>278854</td>\n      <td>portland, oregon, usa</td>\n      <td>NaN</td>\n      <td>usa</td>\n      <td>0</td>\n      <td>0515087122</td>\n      <td>0</td>\n    </tr>\n    <tr>\n      <th>1149769</th>\n      <td>278854</td>\n      <td>portland, oregon, usa</td>\n      <td>NaN</td>\n      <td>usa</td>\n      <td>0</td>\n      <td>0553275739</td>\n      <td>6</td>\n    </tr>\n    <tr>\n      <th>1149770</th>\n      <td>278854</td>\n      <td>portland, oregon, usa</td>\n      <td>NaN</td>\n      <td>usa</td>\n      <td>0</td>\n      <td>0553578596</td>\n      <td>0</td>\n    </tr>\n    <tr>\n      <th>1149771</th>\n      <td>278854</td>\n      <td>portland, oregon, usa</td>\n      <td>NaN</td>\n      <td>usa</td>\n      <td>0</td>\n      <td>0553579606</td>\n      <td>8</td>\n    </tr>\n  </tbody>\n</table>\n<p>1149772 rows × 7 columns</p>\n</div>",
      "text/plain": "         user_id                   location   age country  age_group  \\\n0              2  stockton, california, usa  18.0     usa          3   \n1              7        washington, dc, usa   NaN     usa          0   \n2              8   timmins, ontario, canada   NaN  canada          0   \n3              8   timmins, ontario, canada   NaN  canada          0   \n4              8   timmins, ontario, canada   NaN  canada          0   \n...          ...                        ...   ...     ...        ...   \n1149767   278854      portland, oregon, usa   NaN     usa          0   \n1149768   278854      portland, oregon, usa   NaN     usa          0   \n1149769   278854      portland, oregon, usa   NaN     usa          0   \n1149770   278854      portland, oregon, usa   NaN     usa          0   \n1149771   278854      portland, oregon, usa   NaN     usa          0   \n\n               isbn  rating  \n0        0195153448       0  \n1         034542252       0  \n2        0002005018       5  \n3        0060973129       0  \n4        0374157065       0  \n...             ...     ...  \n1149767  0425163393       7  \n1149768  0515087122       0  \n1149769  0553275739       6  \n1149770  0553578596       0  \n1149771  0553579606       8  \n\n[1149772 rows x 7 columns]"
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df_data = cPickle.load(open('./data/df_data.pkl','rb'))\n",
    "df_data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": "<class 'pandas.core.frame.DataFrame'>\nInt64Index: 1149772 entries, 0 to 1149771\nData columns (total 7 columns):\nuser_id      1149772 non-null int64\nlocation     1149772 non-null object\nage          840283 non-null float64\ncountry      1149772 non-null object\nage_group    1149772 non-null int64\nisbn         1149772 non-null object\nrating       1149772 non-null int64\ndtypes: float64(1), int64(3), object(3)\nmemory usage: 70.2+ MB\n"
    }
   ],
   "source": [
    "df_data.info()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "df_data['age_group'].astype(int)\n",
    "df_data = df_data.drop(['location','age'],axis = 1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": "<class 'pandas.core.frame.DataFrame'>\nInt64Index: 1149772 entries, 0 to 1149771\nData columns (total 5 columns):\nuser_id      1149772 non-null int64\ncountry      1149772 non-null object\nage_group    1149772 non-null int64\nisbn         1149772 non-null object\nrating       1149772 non-null int64\ndtypes: int64(3), object(2)\nmemory usage: 52.6+ MB\n"
    }
   ],
   "source": [
    "df_data.info()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "import random"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "random.seed(123)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "test = random.sample(df_data.index.tolist(), int( len(df_data)* 0.3))\n",
    "df_test = df_data.loc[test]\n",
    "df_train = df_data.loc[~df_data.index.isin(test)]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": "<class 'pandas.core.frame.DataFrame'>\nInt64Index: 804841 entries, 0 to 1149771\nData columns (total 5 columns):\nuser_id      804841 non-null int64\ncountry      804841 non-null object\nage_group    804841 non-null int64\nisbn         804841 non-null object\nrating       804841 non-null int64\ndtypes: int64(3), object(2)\nmemory usage: 36.8+ MB\n"
    }
   ],
   "source": [
    "df_train.info()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [],
   "source": [
    "cPickle.dump(df_test,open('data/df_test.pkl','wb'))\n",
    "cPickle.dump(df_train,open('data/df_train.pkl','wb'))"
   ]
  }
 ]
}